rm(list=ls(all=TRUE))
library(metafor)
library(clubSandwich)
library(dplyr)

setwd("~/Dropbox/Apps/ShareLaTeX/Minimal Effects/replication/data")

data <- read.csv("MPSA Persuasion MetaAnalysis - Candidate Campaign.csv", stringsAsFactors = FALSE)

# Recoding fun
data <- rename(data, CACE = CACE..in.pp., SE = SE..in.pp.)
data$CACE <- data$CACE * 100
data$SE <- data$SE * 100

data$variance <- data$SE^2
data$precision <- 1/data$variance

data$Experiment <- factor(data$Experiment...for.clustering)

# General vs. Primary
data$General <- ifelse(data$Primary.or.General == "General", 1, 0)
data$Primary <- ifelse(data$Primary.or.General == "General", 0, 1)

# Competitiveness
data$Uncompetitive <- ifelse(data$Competitive == 'No', 1, 0)
sum(data[data$Personal==1,]$precision)

#Incumbent vs. Not
data$Incumbent <- ifelse(data$Incumbent %in% c("Yes", "Varies"), 1, 0)

#In-Person Treatment vs. Not
data$Personal <- ifelse(data$Treatment.Mode %in% c("Candidate Canvass", "Canvass",
                                                   "Phone"), 1, 0)
data$CandCanvass <- ifelse(data$Treatment.Mode == 'Candidate Canvass', 1, 0)

# When treatment and survey took place
data$Treatment.days.before.election <- with(data, Days.after.treatment.survey.was.taken - Days.after.election.survey.was.taken)
data$Survey.within.3.weeks.of.election <- as.numeric(data$Days.after.election.survey.was.taken > -21)
data$Treatment.within.3.weeks.of.measure <- as.numeric(data$Days.after.treatment.survey.was.taken < 21)
data$Treatment.within.month.of.election <- as.numeric(data$Treatment.days.before.election < 30)

# Type of Measurement
data$Precinct <- ifelse(data$Measurement.mode == 'Returns', 1, 0)


# Overall
meta <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment), data = data)
coef_test(meta, vcov = "CR2")

# Overall plots
forest(meta, slab = paste0(data$Experiment, ' - ', data$Treatment.Mode), cex = .8, xlab = 'All Studies')


# Looking at just generals w/ treatment close to election day
meta.closegen <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment),
                        data = subset(data, General==1 & Treatment.days.before.election < 60))
pdf('../figures/closegen_funnel.pdf', width = 10, height = 5)
funnel(meta.closegen, refline = 0, ylim = c(0, 11), xlab = 'Estimate in Percentage Points')
dev.off()

# Distribution of t-stats
data$t <- with(data, CACE / SE)


close.ts <- data$t[data$General==1 & data$Treatment.days.before.election < 60]
pdf('../figures/histogram_ts.pdf', width = 5, height = 5)
hist(close.ts, breaks = 18,
     main = 'Distribution of t-statisitcs\nTreatments Within 2 Months of General Elections',
     xlab = 't', xlim = c(-3,3))
abline(v=1.96, col = 'red')
abline(v=-1.96, col = 'red')
lines(seq(-3,3,length=1000), dnorm(seq(-3,3,length=1000))*7, lty = 2)
dev.off()

pdf('../figures/qqplot_ts.pdf', width = 5, height = 5)
qqnorm(close.ts, main = 'Q-Q Plot of t-statistics',
       xlab = "Normal Distribution",
       ylab = "Observed T-Statistics"); qqline(close.ts)
dev.off()

# Forest plot within subsets
subset.forest.plot <- function(sub, title, aliml, alimu, xliml, xlimu, cex = 0.6) {
  meta.sub <- rma.mv(yi = CACE, V = variance, random = list(~1 | Experiment), data = data[sub,])
  print(meta.sub)
  return(forest(meta.sub, slab = paste0(data$Experiment[sub], ' - ', data$Treatment.Mode[sub]),
                cex = cex, xlab = paste0('Estimated Treatment Effect (CACE) in Percentage Points and 95% Confidence Interval\nSubset: ', title),
                alim=c(aliml,alimu), xlim=c(xliml,xlimu)))
}

# Plots
pdf('../figures/ge_treatment_close.pdf', width = 10, height = 6)
subset.forest.plot(data$General == 1 & data$Treatment.days.before.election < 60,
                   'General Elections - Treatment Within 2 Months of Election Day',
                   -20, 20, -40, 32)
dev.off()

pdf('../figures/ge_treatment_close_personal.pdf', width = 10, height = 6)
subset.forest.plot(data$General == 1 & data$Treatment.days.before.election < 60 & data$Personal == 1,
                   'General Elections, Personal - Treatment Within 2 Months of Election Day',
                   -20, 20, -40, 32)
dev.off()

pdf('../figures/ge_treatment_far_decay.pdf', width = 10, height = 2.5)
subset.forest.plot(data$General == 1 & data$Treatment.days.before.election >= 60
                   & data$Days.after.treatment.survey.was.taken > 8,
                   'General Elections - Treatment Prior to 2 Months Before Election Day, Measure Later',
                   -20, 20, -40, 32)
dev.off()

pdf('../figures/ge_treatment_far_immediate.pdf', width = 10, height = 2.75)
subset.forest.plot(data$General == 1 & data$Treatment.days.before.election >= 60
                   & data$Days.after.treatment.survey.was.taken <= 8,
                   'General Elections - Treatment Prior to 2 Months Before Election Day, Measure Immediately',
                   -20, 20, -40, 32, cex = 0.4)
dev.off()

pdf('../figures/primaries.pdf', width = 10, height = 3.2)
subset.forest.plot(data$General == 0, 'Primary Elections - All', -20, 20, -40, 32)
dev.off()


